// pages/member/open_member/open_member.js
const appInstance = getApp()
var url = appInstance.globalData.url
var token = wx.getStorageSync('to_ken')
var order = wx.getStorageSync('Order')
console.log(order)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    circular: true,
    interval: 5000,
    duration: 1000,
    currentSwiper: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getLubo()
  },
  //banner图
  getLubo() {
    var that = this;
    wx.request({
      url: url + '/banner/list',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data);
        that.setData({
          imgUrls: res.data.data
        })
      }
    })
  },
  onChange(e) {
    // console.log(e.detail.current)
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  //获取订单号
  generateOrder() {
    var that = this;
    wx.request({
      url: url + '/order/index',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        pp_token: wx.getStorageSync('to_ken')
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data.code)
        // Order = res.data.data.code;
        // wx.setStorageSync('Order', res.data.data.code)
        that.OrderTap(res.data.data.code)
      }
    })
  },

  //支付接口
  OrderTap(code) {
    wx.request({
      url: url + '/order/wxAppOrder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        pp_token: wx.getStorageSync('to_ken'),
        code: code
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        var timeStamp = res.data.data.timeStamp;
        var nonceStr = res.data.data.nonceStr;
        var packages = res.data.data.package;
        var signType = res.data.data.signType;
        var paySign = res.data.data.paySign;
        //  发起支付
        wx.requestPayment({
          'timeStamp': timeStamp,
          'nonceStr': nonceStr,
          'package': packages,
          'signType': signType,
          'paySign': paySign,
          'success': function(res) {
            console.log(res)
            if (res.errMsg == 'requestPayment:ok') {
              wx.navigateBack({
                delta: 1
              })
              wx.setStorageSync("is_member", 2)
            }
          },
          'fail': function(res) {
            // console.log(res.errMsg)
            if (res.errMsg) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })

      }
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})